function GalleryView() {
	var newData = {};
	var galleryId;
	var galleryContainer;
	
	this.loadNewGallery = function() {
		var pathname = window.location.pathname;
		galleryId = pathname.split('/')[pathname.split('/').length - 1];

		var galleryIndex = parseInt(FullHappyCookie.getCookie('galleryIndex'));
		if (galleryIndex >= 0) {
			var xmlhttp;
			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome,
										// Opera,
				// Safari
				xmlhttp = new XMLHttpRequest();
			} else {// code for IE6, IE5
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.open("GET", "/rest/resources/gallery/" + galleryId + "/"
					+ galleryIndex);
			xmlhttp.send();

			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					if (xmlhttp.status == 200) {
						newData = eval('(' + xmlhttp.responseText + ')');
						display();
						FullHappyCookie.setCookie('galleryIndex',
								galleryIndex + 1, 1);
					} else {
						FullHappyCookie.setCookie('galleryIndex', -1, null);
					}
				}
			};
		}
	};

	var display = function() {
		var container = document.getElementById('gallery_container');
		var productList = createProductList(newData);
		var le = productList.length;
		if(le>0){
			var i = 0;
			container.appendChild(productList[i]);
			var lineHeight = productList[i].offsetHeight;
			container.style.height = container.offsetHeight + lineHeight + 'px';
			for (i = 1; i < le; i++) {
				container.appendChild(productList[i]);
				if (i % 5 == 0) {
					container.style.height = container.offsetHeight + lineHeight
					+ 'px';
				}
			}
		}
	};

	var createProductList = function(productListData) {
		var productList = new Array();
		var productData;

		var le = productListData.length;
		productList[0] = displayProductWithShortInfo1();
		for ( var i = 0; i < le; i++) {
			productData = productListData[i];
			productList[i+1] = displayProductWithShortInfo(productData);
		}
		return productList;
	};

	var displayProductWithShortInfo = function(productData) {
		var article = document.createElement('article');
		var image = document.createElement('img');
		var footer = document.createElement('footer');
		var title = document.createElement('div');
		var price = document.createElement('div');

		var categoryId = productData.categoryId;
		var productId = productData.id;
		var titleTxt = productData.name;
		var shortTitleTxt = titleTxt.split(' ').length>4?titleTxt.substr(0,titleTxt.indexOf(titleTxt.split(' ')[3]))+' ...':titleTxt;
		var imageURL = (productData.imageURLs).split(',')[0];
		var priceTxt = productData.price+' VND';
		
		image.setAttribute('src', imageURL);
		image.setAttribute('alt', titleTxt);
		image.setAttribute('title', titleTxt);
		image.setAttribute('onclick', 'goToProduct(' + categoryId + ','
				+ productId + ')');
		title.setAttribute('class', 'title');
		title.setAttribute('title', titleTxt);
		title.innerHTML = shortTitleTxt;
		title.setAttribute('onclick', 'goToProduct(' + categoryId + ','
				+ productId + ')');
		price.setAttribute('class', 'price');
		price.innerHTML = priceTxt;

		footer.appendChild(title);
		footer.appendChild(price);

		article.appendChild(image);
		article.appendChild(footer);

		return article;
	};
	
	//click go to add product
	var displayProductWithShortInfo1 = function() {
		var article = document.createElement('article');
		var image = document.createElement('img');
		var footer = document.createElement('footer');
		var title = document.createElement('div');
		var price = document.createElement('div');

		var pathname = window.location.pathname;
		var categoryId = pathname.split('/')[pathname.split('/').length - 1];
		var productId = 0;
		var titleTxt = 'Add New Product';
		var shortTitleTxt = 'Add New Product'
		var imageURL = '/images/plus.jpg';
		var priceTxt = '&nbsp;';
		
		image.setAttribute('src', imageURL);
		image.setAttribute('alt', titleTxt);
		image.setAttribute('title', titleTxt);
		image.setAttribute('onclick', 'goToProduct(' + categoryId + ','
				+ productId + ')');
		title.setAttribute('class', 'title');
		title.setAttribute('title', titleTxt);
		title.innerHTML = shortTitleTxt;
		title.setAttribute('onclick', 'goToProduct(' + categoryId + ','
				+ productId + ')');
		price.setAttribute('class', 'price');
		price.innerHTML = priceTxt;

		footer.appendChild(title);
		footer.appendChild(price);

		article.appendChild(image);
		article.appendChild(footer);

		return article;
	};
	

	this.loadNewGalleryInContainer = function(containerId,galleryId1,callBack){
		galleryId = galleryId1;
		var container = document.getElementById(containerId);
		var xmlhttp;
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome,
									// Opera,
			// Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.open("GET", "/rest/resources/gallery/" + galleryId + "/"
				+ 0);
		xmlhttp.send();

		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					newData = eval('(' + xmlhttp.responseText + ')');
					
					var productList = createProductList(newData);
					var le = productList.length;					
					if(le>0){
						var i = 0;
						container.appendChild(productList[i]);
						var lineHeight = productList[i].offsetHeight;
						container.style.height = container.offsetHeight + lineHeight + 'px';
						for (i = 1; i < le; i++) {
							container.appendChild(productList[i]);						
						}					
						
						
						
						$('#'+containerId).carouFredSel({
							circular : false,
							infinite : false,
							auto : false,
							prev : {
								button : '#'+containerId+'_prev',
								key : 'left'
							},
							next : {
								button : '#'+containerId+'_next',
								key : 'right'
							},
							pagination : '#'+containerId+'_pag'
						});
					}
				} else {
					
				}
			}
		};
		
	};

}



function goToProduct(galleryId, productId) {
	window.location.href = '/show_room/' + galleryId + '/' + productId;
}

function goToGallery(galleryId) {
	window.location.href = '/show_room/' + galleryId;
}